﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Marvin.Tests.CsvTrainingDataParserTests
{
    [TestClass]
    public class TestsBehaviourWhenIncorrectDataIsUsed
    {
        private const int FourDimensions = 4;
        private const int FiveDimensions = 5;

        private static readonly List<string> DimensionallyInvalidCsv = new List<string>
            {
                "1.0, 2.0, 3.0, 4.0, 0.0",
                "1.0  ,     2.0, 3.0, 4.0, 0.0",
                "2.1,3.2,1.2,23.12,0.0",
                "1.0, 2.0, 3.0, 4.0, 5.0, 0.0"
            };

        private static readonly List<string> WrongLettersCsv = new List<string>
            {
                "1.0, a, 3.0, 4.0, 0.0",
                "1.0  ,     2.0, 3.0, 4.0, 0.0",
                "2.1,3.2,1.2,d, 0.0",
                "1.0, 2.0, 3.0, 4.0, 5.0, 0.0"
            };



        private static readonly double[][] Expected4DData = new[]
            {
                new [] {1.0,2.0,3.0, 4.0 },
                new [] {1.0,2.0,3.0, 4.0 },
                new [] {2.1,3.2,1.2,23.12}
            };


        private static readonly double[][] Expected5DData = new[]
            {
                new [] {1.0,2.0,3.0, 4.0, 5.0 }
            };

        private static readonly double[][] ExpectedWrongLetterData = new[]
            {
                new [] {1.0,2.0,3.0, 4.0 },
            };

        private CsvTrainingDataParser _parserFourDimensions;
        private CsvTrainingDataParser _parserFiveDimensions;

        [TestInitialize]
        public void CreateAParserForFourFeatures()
        {
            _parserFourDimensions = new CsvTrainingDataParser(FourDimensions);
            _parserFiveDimensions = new CsvTrainingDataParser(FiveDimensions);
        }

        [ExpectedException(typeof(ArgumentException))]
        [TestMethod]
        public void PassingNullToTheParserThrowsAnException()
        {
            _parserFourDimensions.Parse(null);
        }

        [TestMethod]
        public void AFiveDimensionalTrainingExampleInAFourDimensionalDataSetIsSkipped()
        {
            ITrainingSet<double> trainingSet = _parserFourDimensions.Parse(DimensionallyInvalidCsv);
            bool isCorrect = TestHelpers.DeepSequenceEquals(Expected4DData, trainingSet.GetX());
            Assert.IsTrue(isCorrect);
        }

        [TestMethod]
        public void AFourDimensionalTrainingExampleInAFiveDimensionalDataSetIsSkipped()
        {
            ITrainingSet<double> trainingSet = _parserFiveDimensions.Parse(DimensionallyInvalidCsv);
            bool isCorrect = TestHelpers.DeepSequenceEquals(Expected5DData, trainingSet.GetX());
            Assert.IsTrue(isCorrect);
        }

        [TestMethod]
        public void LinesWithWrongCharactersAreSkipped()
        {
            ITrainingSet<double> trainingSet = _parserFourDimensions.Parse(WrongLettersCsv);
            bool isCorrect = TestHelpers.DeepSequenceEquals(ExpectedWrongLetterData, trainingSet.GetX());
            Assert.IsTrue(isCorrect);
        }


    }
}
